package com.example.room_type.controller;

import com.example.core.annotation.HasPerm;
import com.example.model.response.Result;
import com.example.model.room_type.PriceRule;
import com.example.room_type.service.PriceRuleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/priceRule")
public class PriceRuleController {

    @Autowired
    private PriceRuleService priceRuleService;

    @GetMapping("{id}")
    public Result get(@PathVariable("id") Long id) {
        return Result.success(priceRuleService.get(id));
    }

    @GetMapping("listByRoomTypeId")
    public Result getByRoomTypeId(@RequestParam("roomTypeId") Long roomTypeId) {
        return Result.success(priceRuleService.getByRoomTypeId(roomTypeId));
    }

    @GetMapping("/list")
    public Result list(@Valid @RequestBody PriceRule priceRule) {
        return Result.success(priceRuleService.list(priceRule));
    }

    @GetMapping("/page")
    public Result page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, PriceRule priceRule) {
        PageHelper.startPage(pageNum, pageSize);
        List<PriceRule> list = priceRuleService.list(priceRule);
        PageInfo<PriceRule> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @HasPerm("accommodation:priceRule:insert")
    @PostMapping
    public Result insert(@Valid @RequestBody PriceRule priceRule) {
        return Result.success(priceRuleService.insert(priceRule));
    }

    @HasPerm("accommodation:priceRule:update")
    @PutMapping
    public Result update(@RequestBody PriceRule priceRule) {
        return Result.success(priceRuleService.update(priceRule));
    }

    @HasPerm("accommodation:priceRule:delete")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id) {
        return Result.success(priceRuleService.delete(id));
    }
}
